
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bmflocation/bdmap_location_flutter_plugin.dart';
import 'package:flutter_bmflocation/flutter_baidu_location_android_option.dart';
import 'package:flutter_bmflocation/flutter_baidu_location_ios_option.dart';
import 'package:flutter_bmfmap_example/item-1/PersonalMes.dart';
import 'package:flutter_bmfmap_example/item-1/subordinate.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'Mine.dart';
import 'Mission_nearby.dart';
import 'MyFaBu.dart';
import 'Order receiving trends.dart';
import 'appBar.dart';

final pages = [abc(),Order_Receiving_trends(),Mine(todo: Todo(null,null,false),)];
int currentIndex=0;
String address = '广东科学职业技术学院';
String other;
List<String> imgList = ['img/t4.jpg','img/t3.jpg','img/t2.jpg','img/t1.jpg'];
int index = 0;
SwiperController Sc=new SwiperController();  //调整轮播图
int BtmIndex=0;
List Btm = [];
TabController tc = new TabController(length: 2,vsync: ScrollableState());  //vsync 异步处理继承抽象类 TickerProviderStateMixin

void main()=>runApp(MaterialApp(home: HomePage()));

class abc extends StatefulWidget {
  @override
  abcState createState() => new abcState();
}

class abcState extends State<abc> {
  void baibuGps(){
    //创建一个定位对象，后续操作时使用
    LocationFlutterPlugin _locationPlugin = new LocationFlutterPlugin();

// 设置安卓定位参数(按官方文档复制过来就可以了)
    BaiduLocationAndroidOption androidOption = new BaiduLocationAndroidOption();
    androidOption.setCoorType("bd09"); // 设置返回的位置坐标系类型
    androidOption.setIsNeedAltitude(true); // 设置是否需要返回海拔高度信息
    androidOption.setIsNeedAddres(true); // 设置是否需要返回地址信息
    androidOption.setIsNeedLocationPoiList(true); // 设置是否需要返回周边poi信息
    androidOption.setIsNeedNewVersionRgc(true); // 设置是否需要返回最新版本rgc信息
    androidOption.setIsNeedLocationDescribe(true); // 设置是否需要返回位置描述
    androidOption.setOpenGps(true); // 设置是否需要使用gps
    androidOption.setLocationMode(LocationMode.Hight_Accuracy); // 设置定位模式
    androidOption.setScanspan(3000); // 设置发起定位请求时间间隔
    Map androidMap = androidOption.getMap();

//ios定位参数设置
    BaiduLocationIOSOption iosOption = new BaiduLocationIOSOption();
    Map iosdMap = iosOption.getMap();

    _locationPlugin.requestPermission(); //请求定位权限
    _locationPlugin.prepareLoc(androidMap,iosdMap); //ios和安卓定位设置

    _locationPlugin.startLocation(); //开始定位

    //  获取定位结果
    var gps=_locationPlugin.onResultCallback();

    gps.listen((event) {
      setState(() {
        //event就是获取到的结果,要一直监听
        // print(event.values);
        address = event["address"];
        // print('地址:'+address); //打印地址
        // print('状态码：'+event['locType'].toString());      //61为GPS定位成功,详情见官网文档
        // print(event["province"]);  //省份
      });
    });
    // _locationPlugin.stopLocation();//停止定位
  }
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      scrollDirection: Axis.vertical,
      itemCount: 1,
      itemBuilder: (BuildContext context, int index) {
        return
          Container(
            height: 810,
            alignment: Alignment.center,
            child:Column(
              children: [
                //百度地图定位
                Container(
                  child:Row(
                    // mainAxisAlignment: MainAxisAlignment.start,
                    children: <Widget>[
                      Text('  '+address),
                      IconButton(
                        icon: Icon(Icons.cached,),
                        iconSize: 20,
                        onPressed: (){
                          baibuGps();
                        },

                      ),


                    ],
                  ) ,
                ),

                //轮播图Swiper
                Container(
                  height: 200.0,
                  child: Swiper(
                    loop: true,
                    itemCount: imgList.length,  //取IMG数组长度
                    controller: Sc,
                    duration: 1000, //  轮播动画切换秒数，不需要Duration
                    autoplay: true,
                    viewportFraction: 1,
                    pagination: SwiperPagination(
                        margin: EdgeInsets.only(left: 10),
                        builder: DotSwiperPaginationBuilder(
                          color: Colors.grey,
                          activeColor: Colors.black,
                        )
                    ),
                    autoplayDisableOnInteraction: true, //	当用户拖拽的时候，是否停止自动播放
                    onTap: (index){
                      index++;
                      print('现在是第$index页');
                    },
                    itemBuilder: (BuildContext context, int index) {
                      return
                        Container(
                          margin: EdgeInsets.only(left: 5,right: 5),
                          child: ClipRRect(
                            borderRadius: BorderRadius.circular(10),
                            child: Image.asset(
                              imgList[index],width: 320,height: 150,
                              fit: BoxFit.fill,),

                          ),
                        );



                    },
                  ),

                ),


                //三个黄色按钮
                Container(
                  margin: EdgeInsets.only(top: 10),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      //发布跑腿
                      Container(
                        width: 100,
                        height: 100,
                        child: FlatButton(
                          child: Text('发布跑腿'),
                          onPressed: (){
                            Navigator.of(context).push(MaterialPageRoute(
                                builder: (BuildContext context){
                                  return Fabuyy();
                                }
                            )
                            );
                          },
                        ),
                        decoration: BoxDecoration(
                            color: Color.fromRGBO(255, 204, 0, 1),
                            borderRadius: BorderRadius.circular(10)
                        ),
                      ),
                      //附近任务
                      Container(
                        width: 100,
                        height: 100,
                        child: FlatButton(
                          child: Text('附近任务'),
                          onPressed: (){
                            Navigator.of(context).push(MaterialPageRoute(
                                builder: (BuildContext context){
                                  return Mission_nearby();
                                }
                            )
                            );
                          },
                        ),
                        decoration: BoxDecoration(
                            color: Color.fromRGBO(255, 204, 0, 1),
                            borderRadius: BorderRadius.circular(10)
                        ),
                      ),
                      //发布预约
                      Container(
                        width: 100,
                        height: 100,
                        child: FlatButton(
                          child: Text('发布预约'),
                          onPressed: (){
                            Navigator.of(context).push(MaterialPageRoute(
                                builder: (BuildContext context){
                                  return Fabuyy();
                                }
                            )
                            );
                          },
                        ),
                        decoration: BoxDecoration(
                            color: Color.fromRGBO(255, 204, 0, 1),
                            borderRadius: BorderRadius.circular(10)
                        ),
                      ),
                    ],
                  ),
                ),
                DefaultTabController(
                  length: 2,
                  child:TabBar(
                    indicatorColor: Colors.yellow,
                    indicatorSize: TabBarIndicatorSize.label,
                    controller: tc,
                    indicatorWeight: 4,
                    // labelPadding:EdgeInsets.only(top: 5) ,
                    tabs: <Widget>[
                      Tab(
                        child: Text('我的发布',style: TextStyle(color: Colors.black),),
                      ),
                      Tab(
                        child: Text('我的抢单',style: TextStyle(color: Colors.black),),
                      ),

                    ],
                  ), ),

                Container(
                  height: 400,
                  child: TabBarView(
                      controller: tc,
                      children:[
                        Column(
                          children: [
                            GestureDetector(
                              child: Container(
                                color: Colors.white,
                                height: 120,
                                margin: EdgeInsets.only(top: 1),
                                padding: EdgeInsets.only(top: 10,bottom: 0,left: 20,right: 20),
                                child: Container(
                                  child: Column(
                                    children: [
                                      Container(
                                        child: Row(
                                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                          children: [
                                            Container(
                                              child: Row(
                                                children: [
                                                  Text('跑腿',style: TextStyle(color: Colors.black,fontSize: 12,),),
                                                  SizedBox(width: 30,),
                                                  Text('已完成',style: TextStyle(color: Colors.red,fontSize: 12),),
                                                ],
                                              ),
                                            ),
                                            Text('4元',style: TextStyle(color: Colors.black,fontSize: 13),)
                                          ],
                                        ),
                                      ),
                                      Container(
                                        margin: EdgeInsets.only(top: 8),
                                        alignment: Alignment.topLeft,
                                        child: Column(
                                          mainAxisAlignment: MainAxisAlignment.start,
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Container(
                                              margin: EdgeInsets.only(bottom: 8),
                                              child: Text('任务主题（限制20字）...',style: TextStyle(color: Colors.black,fontSize: 13),),
                                            ),
                                            Container(
                                              margin: EdgeInsets.only(bottom: 5),
                                              child: Text('福建省厦门市...',style: TextStyle(color: Colors.grey,fontSize: 11),),
                                            ),
                                            Container(
                                              margin: EdgeInsets.only(bottom: 5),
                                              child: Text('2017-6-20 18:00',style: TextStyle(color: Colors.grey,fontSize: 11),),
                                            ),
                                          ],
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                              onTap: (){
                                Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context){
                                  return ;
                                }));
                              },
                            ),
                            GestureDetector(
                              child: Container(
                                color: Colors.white,
                                height: 120,
                                margin: EdgeInsets.only(top: 1),
                                padding: EdgeInsets.only(top: 10,bottom: 0,left: 20,right: 20),
                                child: Container(
                                  child: Column(
                                    children: [
                                      Container(
                                        child: Row(
                                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                          children: [
                                            Container(
                                              child: Row(
                                                children: [
                                                  Text('跑腿',style: TextStyle(color: Colors.black,fontSize: 12,),),
                                                  SizedBox(width: 30,),
                                                  Text('已完成',style: TextStyle(color: Colors.red,fontSize: 12),),
                                                ],
                                              ),
                                            ),
                                            Text('4元',style: TextStyle(color: Colors.black,fontSize: 13),)
                                          ],
                                        ),
                                      ),
                                      Container(
                                        margin: EdgeInsets.only(top: 8),
                                        alignment: Alignment.topLeft,
                                        child: Column(
                                          mainAxisAlignment: MainAxisAlignment.start,
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Container(
                                              margin: EdgeInsets.only(bottom: 8),
                                              child: Text('任务主题（限制20字）...',style: TextStyle(color: Colors.black,fontSize: 13),),
                                            ),
                                            Container(
                                              margin: EdgeInsets.only(bottom: 5),
                                              child: Text('福建省厦门市...',style: TextStyle(color: Colors.grey,fontSize: 11),),
                                            ),
                                            Container(
                                              margin: EdgeInsets.only(bottom: 5),
                                              child: Text('2017-6-20 18:00',style: TextStyle(color: Colors.grey,fontSize: 11),),
                                            ),
                                          ],
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                              onTap: (){
                                Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context){
                                  return ;
                                }));
                              },
                            ),
                            GestureDetector(
                              child: Container(
                                color: Colors.white,
                                height: 120,
                                margin: EdgeInsets.only(top: 1),
                                padding: EdgeInsets.only(top: 10,bottom: 0,left: 20,right: 20),
                                child: Container(
                                  child: Column(
                                    children: [
                                      Container(
                                        child: Row(
                                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                          children: [
                                            Container(
                                              child: Row(
                                                children: [
                                                  Text('跑腿',style: TextStyle(color: Colors.black,fontSize: 12,),),
                                                  SizedBox(width: 30,),
                                                  Text('已完成',style: TextStyle(color: Colors.red,fontSize: 12),),
                                                ],
                                              ),
                                            ),
                                            Text('4元',style: TextStyle(color: Colors.black,fontSize: 13),)
                                          ],
                                        ),
                                      ),
                                      Container(
                                        margin: EdgeInsets.only(top: 8),
                                        alignment: Alignment.topLeft,
                                        child: Column(
                                          mainAxisAlignment: MainAxisAlignment.start,
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Container(
                                              margin: EdgeInsets.only(bottom: 8),
                                              child: Text('任务主题（限制20字）...',style: TextStyle(color: Colors.black,fontSize: 13),),
                                            ),
                                            Container(
                                              margin: EdgeInsets.only(bottom: 5),
                                              child: Text('福建省厦门市...',style: TextStyle(color: Colors.grey,fontSize: 11),),
                                            ),
                                            Container(
                                              margin: EdgeInsets.only(bottom: 5),
                                              child: Text('2017-6-20 18:00',style: TextStyle(color: Colors.grey,fontSize: 11),),
                                            ),
                                          ],
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                              onTap: (){
                                Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context){
                                  return ;
                                }));
                              },
                            ),
                          ],
                        ),
                        Column(
                          children: [
                            GestureDetector(
                              child: Container(
                                color: Colors.white,
                                height: 120,
                                margin: EdgeInsets.only(top: 1),
                                padding: EdgeInsets.only(top: 10,bottom: 0,left: 20,right: 20),
                                child: Container(
                                  child: Column(
                                    children: [
                                      Container(
                                        child: Row(
                                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                          children: [
                                            Container(
                                              child: Row(
                                                children: [
                                                  Text('跑腿',style: TextStyle(color: Colors.black,fontSize: 12,),),
                                                  SizedBox(width: 30,),
                                                  Text('已完成',style: TextStyle(color: Colors.red,fontSize: 12),),
                                                ],
                                              ),
                                            ),
                                            Text('4元',style: TextStyle(color: Colors.black,fontSize: 13),)
                                          ],
                                        ),
                                      ),
                                      Container(
                                        margin: EdgeInsets.only(top: 8),
                                        alignment: Alignment.topLeft,
                                        child: Column(
                                          mainAxisAlignment: MainAxisAlignment.start,
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Container(
                                              margin: EdgeInsets.only(bottom: 8),
                                              child: Text('任务主题（限制20字）...',style: TextStyle(color: Colors.black,fontSize: 13),),
                                            ),
                                            Container(
                                              margin: EdgeInsets.only(bottom: 5),
                                              child: Text('福建省厦门市...',style: TextStyle(color: Colors.grey,fontSize: 11),),
                                            ),
                                            Container(
                                              margin: EdgeInsets.only(bottom: 5),
                                              child: Text('2017-6-20 18:00',style: TextStyle(color: Colors.grey,fontSize: 11),),
                                            ),
                                          ],
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                              onTap: (){
                                Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context){
                                  return ;
                                }));
                              },
                            ),
                            GestureDetector(
                              child: Container(
                                color: Colors.white,
                                height: 120,
                                margin: EdgeInsets.only(top: 1),
                                padding: EdgeInsets.only(top: 10,bottom: 0,left: 20,right: 20),
                                child: Container(
                                  child: Column(
                                    children: [
                                      Container(
                                        child: Row(
                                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                          children: [
                                            Container(
                                              child: Row(
                                                children: [
                                                  Text('跑腿',style: TextStyle(color: Colors.black,fontSize: 12,),),
                                                  SizedBox(width: 30,),
                                                  Text('已完成',style: TextStyle(color: Colors.red,fontSize: 12),),
                                                ],
                                              ),
                                            ),
                                            Text('4元',style: TextStyle(color: Colors.black,fontSize: 13),)
                                          ],
                                        ),
                                      ),
                                      Container(
                                        margin: EdgeInsets.only(top: 8),
                                        alignment: Alignment.topLeft,
                                        child: Column(
                                          mainAxisAlignment: MainAxisAlignment.start,
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Container(
                                              margin: EdgeInsets.only(bottom: 8),
                                              child: Text('任务主题（限制20字）...',style: TextStyle(color: Colors.black,fontSize: 13),),
                                            ),
                                            Container(
                                              margin: EdgeInsets.only(bottom: 5),
                                              child: Text('福建省厦门市...',style: TextStyle(color: Colors.grey,fontSize: 11),),
                                            ),
                                            Container(
                                              margin: EdgeInsets.only(bottom: 5),
                                              child: Text('2017-6-20 18:00',style: TextStyle(color: Colors.grey,fontSize: 11),),
                                            ),
                                          ],
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                              onTap: (){
                                Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context){
                                  return ;
                                }));
                              },
                            ),
                            GestureDetector(
                              child: Container(
                                color: Colors.white,
                                height: 120,
                                margin: EdgeInsets.only(top: 1),
                                padding: EdgeInsets.only(top: 10,bottom: 0,left: 20,right: 20),
                                child: Container(
                                  child: Column(
                                    children: [
                                      Container(
                                        child: Row(
                                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                          children: [
                                            Container(
                                              child: Row(
                                                children: [
                                                  Text('跑腿',style: TextStyle(color: Colors.black,fontSize: 12,),),
                                                  SizedBox(width: 30,),
                                                  Text('已完成',style: TextStyle(color: Colors.red,fontSize: 12),),
                                                ],
                                              ),
                                            ),
                                            Text('4元',style: TextStyle(color: Colors.black,fontSize: 13),)
                                          ],
                                        ),
                                      ),
                                      Container(
                                        margin: EdgeInsets.only(top: 8),
                                        alignment: Alignment.topLeft,
                                        child: Column(
                                          mainAxisAlignment: MainAxisAlignment.start,
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Container(
                                              margin: EdgeInsets.only(bottom: 8),
                                              child: Text('任务主题（限制20字）...',style: TextStyle(color: Colors.black,fontSize: 13),),
                                            ),
                                            Container(
                                              margin: EdgeInsets.only(bottom: 5),
                                              child: Text('福建省厦门市...',style: TextStyle(color: Colors.grey,fontSize: 11),),
                                            ),
                                            Container(
                                              margin: EdgeInsets.only(bottom: 5),
                                              child: Text('2017-6-20 18:00',style: TextStyle(color: Colors.grey,fontSize: 11),),
                                            ),
                                          ],
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                              onTap: (){
                                Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context){
                                  return ;
                                }));
                              },
                            ),
                          ],
                        ),


                      ] ),
                ),






              ],
            ),
          );
      },

    );
  }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }
}



class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();

}


class _HomePageState extends State<HomePage> with TickerProviderStateMixin {

  List<BottomNavigationBarItem> NavItem=[
    BottomNavigationBarItem(
      title: Text('主页'),
      icon: Icon(Icons.home),
    ),

    BottomNavigationBarItem(
      title: Text('消息'),
      icon: Icon(Icons.message),
    ),

    BottomNavigationBarItem(
      title: Text('我的'),
      icon: Icon(Icons.person),
    ),
  ];
  void _changePage(int index) {
    /*如果点击的导航项不是当前项  切换 */
    if (index != currentIndex) {
      setState(() {
        currentIndex = index;
      });
    }
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        bottomNavigationBar: BottomNavigationBar(
          currentIndex: currentIndex,
          type: BottomNavigationBarType.fixed,
          // currentIndex:BtmIndex ,
          items: NavItem,
          onTap: (index) {
            print(index);
            _changePage(index);
          },
        ),
        appBar: HomePageAppBar(),
        body:
        pages[currentIndex],






      ),

    );
  }



//百度地图定位
  void baibuGps(){
    //创建一个定位对象，后续操作时使用
    LocationFlutterPlugin _locationPlugin = new LocationFlutterPlugin();

// 设置安卓定位参数(按官方文档复制过来就可以了)
    BaiduLocationAndroidOption androidOption = new BaiduLocationAndroidOption();
    androidOption.setCoorType("bd09"); // 设置返回的位置坐标系类型
    androidOption.setIsNeedAltitude(true); // 设置是否需要返回海拔高度信息
    androidOption.setIsNeedAddres(true); // 设置是否需要返回地址信息
    androidOption.setIsNeedLocationPoiList(true); // 设置是否需要返回周边poi信息
    androidOption.setIsNeedNewVersionRgc(true); // 设置是否需要返回最新版本rgc信息
    androidOption.setIsNeedLocationDescribe(true); // 设置是否需要返回位置描述
    androidOption.setOpenGps(true); // 设置是否需要使用gps
    androidOption.setLocationMode(LocationMode.Hight_Accuracy); // 设置定位模式
    androidOption.setScanspan(3000); // 设置发起定位请求时间间隔
    Map androidMap = androidOption.getMap();

//ios定位参数设置
    BaiduLocationIOSOption iosOption = new BaiduLocationIOSOption();
    Map iosdMap = iosOption.getMap();

    _locationPlugin.requestPermission(); //请求定位权限
    _locationPlugin.prepareLoc(androidMap,iosdMap); //ios和安卓定位设置

    _locationPlugin.startLocation(); //开始定位

    //  获取定位结果
    var gps=_locationPlugin.onResultCallback();

    gps.listen((event) {
      setState(() {
        //event就是获取到的结果,要一直监听
        // print(event.values);
        address = event["address"];
        // print('地址:'+address); //打印地址
        // print('状态码：'+event['locType'].toString());      //61为GPS定位成功,详情见官网文档
        // print(event["province"]);  //省份
      });
    });
    // _locationPlugin.stopLocation();//停止定位
  }




  @override
  void initState() {
    tc = TabController(vsync: this,length: 2);
    baibuGps();
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

}

Widget HomePageAppBar(){
  if(currentIndex==0){
    return AppBar(
        backgroundColor: Color.fromRGBO(51, 51, 51, 1),
        titleSpacing: 0.0,
        title: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            FlatButton(
              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
              child: Row(
                children: [
                  Icon(
                    Icons.arrow_back_ios,
                    color: Colors.white,
                  ),
                  Text(
                    '返回',
                    style: TextStyle(color: Colors.white, fontSize: 16),
                  )
                ],
              ),
              onPressed: () {},
            ),
            Container(
              child: Row(
                children: [
                  Text(
                    '跑腿平台',
                    style: TextStyle(fontSize: 16),
                  ),
                  SizedBox(
                    width: 25,
                  )
                ],
              ),
            ),
            IconButton(icon: Icon(Icons.more_horiz), onPressed: () {})
          ],
        )
    );
}else{
    return null;
  }
}

